<template>
  <a-modal
    v-model:visible="visible"
    width="800px"
    title-align="start"
    :title="formModel.dictCode? '详细': '新增'"
    :top="130"
    :align-center=false
    :mask-closable=false
    :draggable=false
  >
    <a-scrollbar style="max-height:600px; overflow:auto;">
      <a-form
        id="addForm"
        ref="formModelRef"
        label-align="left"
        layout="vertical"
        :label-col-props=labelColProps
        :wrapper-col-props=wrapperColProps
        :model="formModel"
        :rules="rules"
      >
        <a-row justify="space-between">
          <a-col :span="11">
            <a-form-item field="dictLabel" label="字典标签">
              <a-input
                v-model="formModel.dictLabel"
                placeholder="请输入字典标签"
                :max-length="30"
                show-word-limit
                allow-clear
              />
            </a-form-item>
          </a-col>
          <a-col :span="11">
            <a-form-item field="dictValue" label="字典数据值">
              <a-input
                v-model="formModel.dictValue"
                placeholder="请输入字典数据值"
                :max-length="30"
                show-word-limit
                allow-clear
              />
            </a-form-item>
          </a-col>
          <a-col :span="11">
            <a-form-item field="dictSort" label="排序">
              <a-input-number
                v-model="formModel.dictSort"
                mode="button"
                :default-value="0"
                :min="0"
                :max="999"
              />
            </a-form-item>
          </a-col>
          <a-col :span="11">
            <a-form-item field="cssClass" label="样式">
              <a-input
                v-model="formModel.cssClass"
                placeholder="请输入样式"
                :max-length="50"
                show-word-limit
                allow-clear
              />
            </a-form-item>
          </a-col>
          <a-col :span="11">
            <a-form-item field="listClass" label="列表样式">
              <a-input
                v-model="formModel.listClass"
                placeholder="请输入列表样式"
                :max-length="50"
                show-word-limit
                allow-clear
              />
            </a-form-item>
          </a-col>
          <a-col :span="11">
            <a-form-item field="remark" label="备注">
              <a-textarea
                v-model="formModel.remark"
                placeholder="请输入备注"
                :max-length="20"
                show-word-limit
                auto-size
              />
            </a-form-item>
          </a-col>
          <a-col :span="11">
            <a-form-item field="isDefault" label="默认选中">
              <a-switch
                v-model="formModel.isDefault"
                type="round"
                :checked-value="'Y'"
                :unchecked-value="'N'"
              >
                <template #checked>是</template>
                <template #unchecked>否</template>
              </a-switch>
            </a-form-item>
          </a-col>
          <a-col :span="11">
            <a-form-item field="status" label="状态">
              <a-switch
                v-model="formModel.status"
                type="round"
                :checked-value="'0'"
                :unchecked-value="'1'"
              >
                <template #checked>正常</template>
                <template #unchecked>冻结</template>
              </a-switch>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-scrollbar>
    <template #footer>
      <a-row justify="end">
        <a-button-group>
          <a-button :disabled="loading" @click="cancel">
            取消
          </a-button>
          <a-button
            type="primary"
            :loading="loading"
            :disabled="hasUpdatePermission"
            @click="submit"
          >
            确定
          </a-button>
        </a-button-group>
      </a-row>
    </template>
  </a-modal>
</template>

<script lang="ts" setup>
import { ref, defineEmits, defineExpose, computed } from 'vue';
import { addDictData, updateDictData, detailDictData, SysDictData } from '@/api/system/dictData';
import { hasPermission } from '@/utils/permission';
import { rules, permission } from '@/views/system/dict/dialog/config';
import { Message, ValidatedError } from '@arco-design/web-vue';
import { labelColProps, wrapperColProps } from '@/components/dialog-add/config';
import useLoading from '@/hooks/loading';

const emits = defineEmits(['submit']);

const { loading, setLoading } = useLoading(false);

const formModelRef = ref();

const visible = ref<boolean>(false);

const formDisable = ref<boolean>(false);

const formModel = ref<SysDictData>({});

const hasUpdatePermission = computed(() => {
  return formModel.value.dictCode && !hasPermission([permission.PERMISSION_DATA_UPDATE]);
});

const submit = async () => {
  formModelRef.value.validate((errors: undefined | Record<string, ValidatedError>) => {
    if (errors) {
      return;
    }
    setLoading(true);
    if (formModel.value.dictCode) {
      updateDictData(formModel.value).then(() => {
        submitSuccess();
      }).finally(() => {
        setLoading(false);
      });
      return;
    }
    addDictData(formModel.value).then(() => {
      submitSuccess();
    }).finally(() => {
      setLoading(false);
    });
  });
};

const submitSuccess = () => {
  cancel();
  emits('submit');
  Message.success('操作成功');
};

const open = async (record: any, disable: boolean, dictType: string) => {
  visible.value = true;
  if (record && record.dictCode) {
    const { data } = await detailDictData(record.dictCode);
    formModel.value = data;
    formModel.value.dictSort = Number(data.dictSort);
  } else {
    formModel.value = { dictType, status: '0', isDefault: 'N', dictSort: 0 };
  }
  visible.value = true;
  formDisable.value = disable;
  formModelRef.value.clearValidate();
  setLoading(false);
};

const cancel = () => {
  visible.value = false;
};

defineExpose({
  open
});
</script>

<script lang="ts">
export default {
  name: 'DictDataDialogAdd'
};
</script>

<style scoped>

</style>